package com.chain.BrotherExchange.ui.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.model.LegalTenderAppealOrderInfo;
import com.chain.BrotherExchange.data.model.LegalTenderOrderList;
import com.chain.BrotherExchange.im.ui.WebSocketActivity;
import com.chain.BrotherExchange.ui.activity.LegalTenderAppealActivity;
import com.chain.BrotherExchange.ui.activity.LegalTenderBuyOrderDetailsActivity;
import com.chain.BrotherExchange.ui.activity.LegalTenderSellOrderDetailsActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 法币订单
 */
public class LegalTenderOrderListAdapter extends RecyclerView.Adapter<LegalTenderOrderListAdapter.ViewHolder> {

    private OnItemClickListener mOnPaymentListener;
    private OnItemClickListener mCancelOrderListener;
    private OnItemClickListener mOrderReleaseListener;
    private OnItemClickListener mOnCancelAppealListener;
    private List<LegalTenderOrderList.DataBean.ListBean> mOrderList;

    public interface OnItemClickListener {
        void onClick(int position, int orderId);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_legal_tender_order, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull LegalTenderOrderListAdapter.ViewHolder holder, int i) {
        Context context = holder.itemView.getContext();
        Resources resources = context.getResources();
        LegalTenderOrderList.DataBean.ListBean bean = mOrderList.get(i);
        holder.orderNumberText.setText(resources.getString(R.string.text_order_number) + bean.getOrder_id());
        holder.orderStateText.setText(bean.getStateText());
        holder.coinTypeAndDirectionText.setText(bean.getToken_name() + "/" + bean.getDirectionText());
        holder.unitPriceText.setText(bean.getPriceText());
        holder.orderNumberTitleText.setText(String.format(resources.getString(
                R.string.placeholder_order_number), bean.getToken_name()));
        holder.purchaseNumberText.setText(bean.getNumText());
        holder.totalNumberText.setText(bean.getTotalPrice());
        holder.orderDateText.setText(bean.getCreated_time());

        // 订单详情
        holder.itemView.setOnClickListener(v -> {
            if (bean.getAd_type() == C.API_PURCHASE) {
                LegalTenderBuyOrderDetailsActivity.launch(
                        holder.itemView.getContext(), String.valueOf(bean.getId()),
                        bean.getAppeal_states(), bean.getAppeal_id(), i);
            } else {
                LegalTenderSellOrderDetailsActivity.launch(
                        holder.itemView.getContext(), String.valueOf(bean.getId()),
                        bean.getAppeal_states(), bean.getAppeal_id(), i);
            }
        });

        if (bean.isShowActionLayout()) {
            holder.cancelOrderLayout.setVisibility(View.VISIBLE);
        } else {
            holder.cancelOrderLayout.setVisibility(View.GONE);
        }

        // 联系卖家或者买家
        if (bean.isShowContractSeller() || bean.isShowContactBuyer()) {
            holder.orderContractText.setVisibility(View.VISIBLE);
            holder.orderContractText.setOnClickListener(v -> WebSocketActivity.launch(v.getContext(), bean));

            if (bean.isShowContactBuyer()) {
                holder.orderContractText.setText(R.string.text_contact_buyer);
            } else {
                holder.orderContractText.setText(R.string.text_contact_seller);
            }
        } else {
            holder.orderContractText.setVisibility(View.GONE);
        }


        // 支付或放行或申诉
        if (bean.isShowPayment() || bean.isShowOrderRelease() || bean.isShowCanAppeal()) {
            holder.orderActionText.setVisibility(View.VISIBLE);
            if (bean.isShowPayment()) {
                holder.orderActionText.setText(R.string.text_pay_immediately);
                holder.orderActionText.setOnClickListener(v -> mOnPaymentListener.onClick(i, bean.getId()));
            } else if (bean.isShowOrderRelease()) {
                holder.orderActionText.setText(R.string.text_confirm_release);
                holder.orderActionText.setOnClickListener(v -> mOrderReleaseListener.onClick(i, bean.getId()));
            } else if (bean.isShowCanAppeal()) {
                holder.orderActionText.setText(R.string.appeal_immediately);
                holder.orderActionText.setOnClickListener(v ->
                        LegalTenderAppealActivity.launch(context, new LegalTenderAppealOrderInfo(
                                bean.getToken_name(), bean.getOrder_id(), bean.getAd_type(), bean.getToken_id()
                        ), i));
            }
        } else {
            holder.orderActionText.setVisibility(View.GONE);
        }

        // 取消订单
        if (bean.isShowCancelOrder() || bean.isShowCancelAppeal()) {
            holder.cancelOrderText.setVisibility(View.VISIBLE);
            if (bean.isShowCancelOrder()) {
                holder.cancelOrderText.setText(R.string.cancel_order);
                holder.cancelOrderText.setOnClickListener(v -> mCancelOrderListener.onClick(i, bean.getId()));
            } else if (bean.isShowCancelAppeal()) {
                holder.cancelOrderText.setText(R.string.cancel_appeal);
                holder.cancelOrderText.setOnClickListener(v -> mOnCancelAppealListener.onClick(i, bean.getAppeal_id()));
            }
        } else {
            holder.cancelOrderText.setVisibility(View.GONE);
        }
    }

    public void setCancelAppealListener(OnItemClickListener onItemClickListener) {
        this.mOnCancelAppealListener = onItemClickListener;
    }

    public void setPaymentListener(OnItemClickListener onItemClickListener) {
        this.mOnPaymentListener = onItemClickListener;
    }

    public void setCancelOrderListener(OnItemClickListener onCancelOrderListener) {
        this.mCancelOrderListener = onCancelOrderListener;
    }

    public void setOrderReleaseListener(OnItemClickListener onOrderReleaseListener) {
        this.mOrderReleaseListener = onOrderReleaseListener;
    }

    public LegalTenderOrderList.DataBean.ListBean getItem(int position) {
        return mOrderList.get(position);
    }

    @Override
    public int getItemCount() {
        return mOrderList != null ? mOrderList.size() : 0;
    }

    public void addOrder(List<LegalTenderOrderList.DataBean.ListBean> source) {
        mOrderList.addAll(source);
        notifyDataSetChanged();
    }

    public void setOrder(List<LegalTenderOrderList.DataBean.ListBean> source) {
        mOrderList = source;
        notifyDataSetChanged();
    }

    public void changeOrderState(int state, int position) {
        mOrderList.get(position).setStates(state);
        notifyItemChanged(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.order_number_text) TextView orderNumberText;
        @Bind(R.id.order_state_text) TextView orderStateText;
        @Bind(R.id.coin_type_and_direction_text) TextView coinTypeAndDirectionText;
        @Bind(R.id.unit_price_text) TextView unitPriceText;
        @Bind(R.id.order_number_title_text) TextView orderNumberTitleText;
        @Bind(R.id.purchase_number_text) TextView purchaseNumberText;
        @Bind(R.id.total_number_text) TextView totalNumberText;
        @Bind(R.id.order_date_text) TextView orderDateText;
        @Bind(R.id.cancel_order_text) TextView cancelOrderText;
        @Bind(R.id.order_action_text) TextView orderActionText;
        @Bind(R.id.cancel_order_layout) View cancelOrderLayout;
        @Bind(R.id.order_contact_text) TextView orderContractText;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
